//
//  ZYAudientListSortViewController.m
//  AudientList
//
//  Created by zhangweiwei on 16/6/15.
//  Copyright © 2016年 Erica. All rights reserved.
//

#import "ZYAudientListSortViewController.h"

#import "KCCategory.h"


typedef enum {
    PresentType,//弹出
    DissType//消失
}TransitionType;



@interface PresentModal : NSObject<UIViewControllerAnimatedTransitioning>

@property (nonatomic,assign) TransitionType type;

+(instancetype)transitionWithType:(TransitionType)type;

@end

@implementation PresentModal



+(instancetype)transitionWithType:(TransitionType)type
{
    
    PresentModal *transition = [[self alloc]init];
    transition.type = type;
    return transition;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    if (self.type == PresentType ) {
        [self presentAnimation:transitionContext];
    }else{
        [self dismissAnimation:transitionContext];
    }
    
}


- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    
    UIView *containerView = [transitionContext containerView];
    
    
    [containerView addSubview:toVC.view];
    
    
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    
    
    
}

//实现dismiss动画逻辑代码
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    [fromVC.view removeFromSuperview];
    
    
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    
}

@end



@interface ZYAudientListSortViewController ()<UITableViewDelegate, UITableViewDataSource, UIViewControllerTransitioningDelegate>
@property (nonatomic, weak) UIView *targetView;

@property (nonatomic, strong) NSArray *sortItems;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ZYAudientListSortViewController

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.layer.anchorPoint = CGPointMake(0.5, 0);
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight = 44;
        
        
    }
    return _tableView;
}

- (instancetype)initWithTargetView:(UIView *)targetView sortItems:(NSArray *)sortItems
{
    if (self = [super init]) {
        self.targetView = targetView;
        self.sortItems = sortItems;
        
        //自定义modal
        self.transitioningDelegate = self;
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tableView.transform = CGAffineTransformMakeScale(1, 0);
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        self.tableView.transform = CGAffineTransformIdentity;
        
    } completion:nil];
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    CGRect targetFrame = [self.targetView.superview convertRect:self.targetView.frame toView:nil];
    
    self.tableView.kc_x = targetFrame.origin.x;
    self.tableView.kc_y = CGRectGetMaxY(targetFrame);
    self.tableView.kc_width = targetFrame.size.width;
    self.tableView.kc_height = self.tableView.rowHeight * self.sortItems.count;
    
}

#pragma mark -UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sortItems.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *reuseID = @"UITableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor darkGrayColor];
    }
    
    cell.textLabel.text = self.sortItems[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    !self.DidSelectedItemAtIndex ? : self.DidSelectedItemAtIndex(indexPath.item);
    
    [self dismiss];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self dismiss];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
        
        self.tableView.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}


#pragma mark-UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    //初始化presentType
    return [PresentModal transitionWithType:PresentType];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    //初始化dismissType
    return  [PresentModal transitionWithType:DissType];
}


@end
